home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Apple Developer Connection Student Program
/
ADC Tools Sampler CD Disk 3 1999.iso
/
Documentation
/
Books
/
Learn Java on the Macintosh
/
Learn Java Projects
/
12.04 - Payroll
/
Payroll.java
< prev
next >
Wrap
Text File
|
1996-04-22
|
5KB
|
185 lines
/* -----------------------------------------------------------
This illustrates a few standard classes and basic applet behavior.
Enter an employee number for an employee into a text field.
If this employee exists, the applet will find the employee and
display the employee's payroll information. Otherwise, the applet
will create a new employee and add the employee to the database.
Java's classes: Applet (applet)
TextField (awt) to enter new employee data
Label (awt) read-only text
GridLayout (awt) aligns by columns and rows
Event (awt) user-generated action
Hashtable (util) database
String (lang) text
Integer (lang) number
Custom classes: Payroll
Employee payroll information
----------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;
import java.util.*;
public class Payroll extends Applet {
Hashtable db;
TextField textFieldEmployee;
TextField textFieldWage;
TextField textFieldHours;
Label labelEarned;
Employee current;
/* Create user interface needed by this applet. */
public void init() {
// Create the employee database.
db = new Hashtable();
// Arrange the user interface in a grid.
setLayout(new GridLayout(4,2)); // 4 rows, 2 columns
// 1st row.
add(new Label("Employee number:"));
textFieldEmployee = new TextField(20); // 20 columns wide
add(textFieldEmployee);
// 2nd row.
add(new Label("Hourly wage:"));
textFieldWage = new TextField(20); // 20 columns wide
add(textFieldWage);
// 3rd row.
add(new Label("Hours worked:"));
textFieldHours = new TextField(20); // 20 columns wide
add(textFieldHours);
// 4th row.
add(new Label("Earned income:"));
labelEarned = new Label();
add(labelEarned);
setCurrent(null);
}
/** Handle events that propogate to the applet. This will include new text field data. */
public boolean action(Event e, Object arg) {
Employee employee;
int number;
// Create/retrieve the employee according to the number the user entered.
if (e.target == textFieldEmployee) {
number = intFromTextField(textFieldEmployee);
employee = findEmployee(number);
// Create a new employee if the employee is not already there.
if (employee == null)
employee = addNew(number);
// Display this employee's payroll information.
setCurrent(employee);
// Set the hourly wage for the current employee.
} else if (e.target == textFieldWage) {
if (current != null) {
current.hourlyWage = intFromTextField(textFieldWage);
recalcEarned();
}
// Set the number of hours worked for the current employee.
} else if (e.target == textFieldHours) {
if (current != null) {
current.hoursWorked = intFromTextField(textFieldHours);
recalcEarned();
}
}
return super.action(e, arg);
}
/** This is a utility routine to retrieve an integer from a text field. */
int intFromTextField(TextField tf) {
String s;
int value;
s = tf.getText();
try {
value = Integer.parseInt(s);
} catch (Exception e) {
value = 0;
setCurrent(null);
}
return value;
}
/** Do a database lookup using the employee's number as the key. */
Employee findEmployee(int number) {
return (Employee)db.get(new Integer(number));
}
/** Set the text fields to display the correct information for the current employee. */
void setCurrent(Employee e) {
current = e;
// If there isn't a current employee, initialize the fields to 0.
if (e == null) {
textFieldEmployee.setText("0");
textFieldWage.setText("0");
textFieldHours.setText("0");
} else {
textFieldWage.setText(Integer.toString(current.hourlyWage));
textFieldHours.setText(Integer.toString(current.hoursWorked));
}
recalcEarned();
}
/** Create a new employee and add it to the database */
Employee addNew(int number) {
Employee e = new Employee();
e.idNumber = number;
e.hourlyWage = 0;
e.hoursWorked = 0;
db.put(new Integer(number), e); // Add to the database using (key, value)
setCurrent(e);
return e;
}
/** Recalculate the text to display in the "Earned income:" label. */
void recalcEarned() {
int earned;
if (current != null)
earned = current.earnedIncome();
else
earned = 0;
labelEarned.setText(Integer.toString(earned));
}
}
/** Maintain payroll information for an employee. */
class Employee {
int idNumber;
int hourlyWage;
int hoursWorked;
int earnedIncome() {
return hourlyWage * hoursWorked;
}
}